<?php

namespace App\Repository\Repositories;

use App\Models\DrawRecords;
use App\Models\RafflePrizes;
use App\Models\StoreProduct;
use App\Models\User;
use App\Repository\RepositoryInterface\LotteryInterface;
use Illuminate\Validation\ValidationException;

class LotteryRepositories implements LotteryInterface
{
    /**
     * @return mixed
     * 查询参与抽奖商品
     */
    public function lotteryGoods()
    {
        try {
            $lottery_data = RafflePrizes::get();
            return $lottery_data;
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param $user_id
     * @param $store_name
     * @param $price
     * @return bool|string
     * 抽奖记录
     */
    public function drawRecords($user_id,$user_name,$store_name,$price){
        try {
            $draw_records = DrawRecords::insert([
                'user_id' => $user_id,
                'user_name' => $user_name,
                'store_name' => $store_name,
                'price' => $price,
                'add_time' => time()
            ]);
            if ($draw_records){
                return true;
            }else{
                return false;
            }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param $goods_id
     * @return false|string
     * 检索商品
     */
    public function searchGoods($goods_id){
        try {
            $search_goods = RafflePrizes::where('id',$goods_id)->first();
            if ($search_goods){
                return $search_goods;
            }else{
                return false;
            }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param $user_id
     * @return false|string
     * 抽奖记录查询
     */
    public function drawRecordsQuery($user_id){
        try {
            $drawRecordsQuery = DrawRecords::where('user_id',$user_id)->orderBy('id','desc')->limit(15)->get();
            $drawRecordsQuery = json_decode($drawRecordsQuery,true);
            sort($drawRecordsQuery);
            if ($drawRecordsQuery){
                return $drawRecordsQuery;
            }else{
                return false;
            }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param $user_id
     * @return false|string
     * 查询中奖用户
     */
    public function searchUser($user_id){
        try {
            $search_user = User::where('uid',$user_id)->first();
            if ($search_user){
                return $search_user;
            }else{
                return false;
            }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param $draw_records_id
     * @return bool|string
     * 奖品删除
     */
    public function drawRecordsDel($draw_records_id){
        try {
            $draw_records_reg = DrawRecords::destroy($draw_records_id);
            if ($draw_records_reg){
                return true;
            }else{
                return false;
            }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }
}
